package zhongbo.javaweb.instagram.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;

/**
 * 说明：spring web 相关的工具类
 *
 * @author 钟波notebook
 * @since 2020年03月02日 星期一 09:53:23
 */
@Slf4j
public class SpringWebUtil {

    /**
     * 获取上下文中 Request 信息
     *
     * @return request
     */
    public static HttpServletRequest getContextRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getRequest();
        }
        return null;
    }

    public static String getRequestInfo(HttpServletRequest request) {
        if (null != (request)) {
            String method = request.getMethod();
            StringBuffer requestUrl = request.getRequestURL();
            String requestUri = request.getRequestURI();
            URL url = null;
            try {
                url = new URL(requestUrl.toString());
            } catch (MalformedURLException e) {
                log.error("url解析失败:", e);
            }
            String protocol = request.getProtocol();
            String remoteAddr = request.getRemoteAddr();
            int remotePort = request.getRemotePort();
            String localAddr = request.getLocalAddr();
            int localPort = request.getLocalPort();
            HttpHeaders requestHeaders = getRequestHeaders(request);
            String contentType = request.getContentType();
            int len = request.getContentLength();
            Locale locale = request.getLocale();
            HttpSession session = request.getSession();
            String sessionId = session.getId();
            log.debug("params are requestUri: {} url: {} protocol: {} requestHeaders: {} locale: {} sessionId: {}", requestUri, url, protocol, requestHeaders, locale, sessionId);
            String format = "请求信息: Method:%s, URL:%s; remote:%s:%s, local:%s%s; contentType:%s length:%s";
            return String.format(format, method, requestUrl, remoteAddr, remotePort, localAddr, localPort, contentType, len);
        }
        return "";
    }

    /**
     * 获取请求 headers
     *
     * @param request 请求
     * @return HttpHeaders headers
     */
    public static HttpHeaders getRequestHeaders(HttpServletRequest request) {
        HttpHeaders headers = new HttpHeaders();
        if (null != (request)) {
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                Enumeration<String> values = request.getHeaders(name);
                List<String> valueList = new ArrayList<>(16);
                while (values.hasMoreElements()) {
                    String value = values.nextElement();
                    valueList.add(value);
                }
                headers.put(name, valueList);
            }
        }
        return headers;
    }
}
